package cn.matezk.lianlian.service.impl;

import cn.matezk.lianlian.dao.DeviceBindDao;
import cn.matezk.lianlian.dao.DeviceDao;
import cn.matezk.lianlian.model.Device;
import cn.matezk.lianlian.model.DeviceBind;
import cn.matezk.lianlian.service.IDeviceBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceBindService implements IDeviceBindService {
    @Autowired
    private DeviceBindDao deviceBindDao;
    @Autowired
    private DeviceDao deviceDao;

    @Override
    @Cacheable(cacheNames = "appKey.bind", unless = "#result == null", key = "#appKey.concat('.').concat(#productId).concat('/').concat(#deviceName)")
    public DeviceBind selectByAppKeyProductDevice(String appKey, String productId, String deviceName) {
        DeviceBind bind = deviceBindDao.selectByAppKeyProductDevice(appKey, productId, deviceName);
        return bind;
    }

    @Override
    public List<Device> selectProductDeviceByUnionId(String unionId, String productId) {
        return deviceDao.selectProductDeviceByUnionId(unionId, productId);
    }

    @Override
    @CacheEvict(cacheNames = "appKey.bind", key = "#appKey.concat('.').concat(#productId).concat('/').concat(#deviceName)")
    public void unbind(String appKey, String productId, String deviceName) {
        deviceBindDao.unbind(productId, deviceName);
    }
}
